﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAOMall;

namespace DTOMall
{
    public class GallerysDTO : DBTool, ICommon
    {
        public bool insert(object obj)
        {
            if (obj == typeof(Gallerys))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Gallerys ga = (Gallerys)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Insert_Gallerys";
                            cmd.Parameters.AddWithValue("@shop_id", ga.shopID);
                            cmd.Parameters.AddWithValue("@title", ga.title);
                            cmd.Parameters.AddWithValue("@titlePlain", ga.title_plain);
                            cmd.Parameters.AddWithValue("@description", ga.description);
                            cmd.Parameters.AddWithValue("@content", ga.content);
                            cmd.Parameters.AddWithValue("@image", ga.image);
                            cmd.Parameters.AddWithValue("@images", ga.images);
                            cmd.Parameters.AddWithValue("@createdDate", ga.created_date);
                            cmd.Parameters.AddWithValue("@status", ga.status);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

            }
            else
            {
                return false;
            }
        }

        public bool update(object obj)
        {
            if (obj == typeof(Products))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Gallerys ga = (Gallerys)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Update_Gallerys";
                            cmd.Parameters.AddWithValue("@id", ga.id);
                            cmd.Parameters.AddWithValue("@shop_id", ga.shopID);
                            cmd.Parameters.AddWithValue("@title", ga.title);
                            cmd.Parameters.AddWithValue("@titlePlain", ga.title_plain);
                            cmd.Parameters.AddWithValue("@description", ga.description);
                            cmd.Parameters.AddWithValue("@content", ga.content);
                            cmd.Parameters.AddWithValue("@image", ga.image);
                            cmd.Parameters.AddWithValue("@images", ga.images);
                            cmd.Parameters.AddWithValue("@status", ga.status);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                return false;
            }
        }

        public bool delete(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "Delete_Gallerys";
                        cmd.Parameters.AddWithValue("@id", id);
                        object o = cmd.ExecuteScalar();
                        if (o == typeof(int))
                        {
                            return ((int)o > 0) ? true : false;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public System.Data.DataTable getAll()
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_All_Gallerys";
                        DataTable dt = GetTable(cmd);
                        return dt;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public object getByID(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_Gallerys_By_ID";
                        cmd.Parameters.AddWithValue("@id", id);
                        DataTable dt = GetTable(cmd);
                        Gallerys ga = new Gallerys();
                        ga.id = id;
                        ga.shopID = Convert.ToInt32(dt.Rows[0]["shop_id"].ToString());
                        ga.title_plain = dt.Rows[0]["title_plain"].ToString();
                        ga.title = dt.Rows[0]["title"].ToString();
                        ga.description = dt.Rows[0]["decription"].ToString();
                        ga.content = dt.Rows[0]["content"].ToString();
                        ga.image = dt.Rows[0]["image"].ToString();
                        ga.images = dt.Rows[0]["images"].ToString();
                        ga.created_date = DateTimeLib.Convert2Date(dt.Rows[0]["created_date"].ToString());
                        ga.status = Convert.ToInt32(dt.Rows[0]["status"].ToString());

                        return ga;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
